package com.moxiao.yfjh.module.activity.service.activityVerificate;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.ActivityVerificateCreateReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.ActivityVerificateExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.ActivityVerificatePageReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityVerificate.vo.ActivityVerificateUpdateReqVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppVerificationMeltVo;
import com.moxiao.yfjh.module.activity.convert.activityVerificate.ActivityVerificateConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityVerificate.ActivityVerificateDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityenrollverificate.ActivityEnrollVerificateDO;
import com.moxiao.yfjh.module.activity.dal.mysql.activityVerificate.ActivityVerificateMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.VERIFICATE_NOT_EXISTS;

/**
 * 相亲核销记录信息 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class ActivityVerificateServiceImpl implements ActivityVerificateService {

    @Resource
    private ActivityVerificateMapper verificateMapper;

    @Override
    public Long createVerificate(ActivityVerificateCreateReqVO createReqVO) {
        // 插入
        ActivityVerificateDO verificate = ActivityVerificateConvert.INSTANCE.convert(createReqVO);
        verificateMapper.insert(verificate);
        // 返回
        return verificate.getId();
    }

    @Override
    public void updateVerificate(ActivityVerificateUpdateReqVO updateReqVO) {
        // 校验存在
        validateVerificateExists(updateReqVO.getId());
        // 更新
        ActivityVerificateDO updateObj = ActivityVerificateConvert.INSTANCE.convert(updateReqVO);
        verificateMapper.updateById(updateObj);
    }

    @Override
    public void deleteVerificate(Long id) {
        // 校验存在
        validateVerificateExists(id);
        // 删除
        verificateMapper.deleteById(id);
    }

    private void validateVerificateExists(Long id) {
        if (verificateMapper.selectById(id) == null) {
            throw exception(VERIFICATE_NOT_EXISTS);
        }
    }

    @Override
    public ActivityVerificateDO getVerificate(Long id) {
        return verificateMapper.selectById(id);
    }

    @Override
    public List<ActivityVerificateDO> getVerificateList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return verificateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ActivityVerificateDO> getVerificatePage(ActivityVerificatePageReqVO pageReqVO) {
        return verificateMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ActivityVerificateDO> getVerificateList(ActivityVerificateExportReqVO exportReqVO) {
        return verificateMapper.selectList(exportReqVO);
    }

    @Override
    public BigDecimal getRefundPrice(Long activityId, Long userId) {
        return verificateMapper.getRefundPrice(userId, activityId, (byte) 1);
    }

    @Override
    public List<AppVerificationMeltVo> getVerList(Long activityId, Long userId) {
        List<AppVerificationMeltVo> list = verificateMapper.getVerList(activityId, userId);
        return list;
    }

    @Override
    public List<AppVerificationMeltVo> getCreatorVerList(Long activityId, Long userId) {
        List<AppVerificationMeltVo> creatorList = verificateMapper.getCreatorVerList(activityId, userId);
        return creatorList;
    }

    @Override
    public List<AppVerificationMeltVo> getCodeList(Long activityId, Long userId, Long enrollId) {
        return verificateMapper.getCodeList(activityId, userId, enrollId);
    }


    @Override
    public ActivityEnrollVerificateDO getCode(String code) {
        return verificateMapper.getCode(code);
    }
}
